<template>
  <div class="container">
    <!-- <Header></Header> -->
    <div v-show="ID===null" class="NOLogin">
      <h1>抱歉你还没有登录</h1>
      <h1><van-button type="primary" @click="Login">点击登录</van-button></h1>
    </div>
    <div class="list-wrap" v-show="ID!==null">
      <vo-pages :data="list"
                @pullingUp="pullingUp"
                @pullingDown="pullingDown"
                :loadedAll="loadedAll">
        <ul class="article-list">
          <li class="article"
              v-for="article in list"
              :key="article.id">
            <div class="left">
              <img v-lazy='imageUrl'
                  alt="thumb" />
            </div>
            <div class="right" >
              <!-- <router-link :to="'/Adata/' +  article.id">{{ article.title }}</router-link> -->
              <p @click="chuancan(article._id)">{{ article.title }}</p>
              <p class="more-info">
                <span class="author">分类：{{ article.cate[0].title }}</span>
                <span class="time">时间：2020-2-12</span>
              </p>
            </div>
          </li>
        </ul>
      </vo-pages>
    </div>
    <!-- <footer-tabbar/> -->
  </div>
</template>
<script>
import { Tabbar, TabbarItem, Button } from 'vant'
import { ArticleUser } from 'api/article'
import VoPages from 'vo-pages'
import 'vo-pages/lib/vo-pages.css'
import eventBus from '@/eventBus.js'
export default {
  name: 'Article',
  data () {
    return {
      active: 1,
      list: [],
      total: 0,
      page: 1,
      loadedAll: false,
      imageUrl: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3642481453,1486938825&fm=26&gp=0.jpg',
       ID: ''
    }
  },
  mounted () {
    this.getList()
  },
  components: {
    [Tabbar.name]: Tabbar,
    [TabbarItem.name]: TabbarItem,
    [Button.name]: Button,
    VoPages,
  },
  created() {
      eventBus.$on('list', (message) => {
        // 一些操作，message就是从top组件传过来的值
        this.list = message
        console.log(message);
    })
    // 获取用户ID
    this.ID = localStorage.getItem('user');
    console.log(this.ID);
  },
  methods: {
    pullingDown () {
      this.beforePullDown = false
      this.page = 1
      this.getList(false)
    },
    Login() {
        this.$router.push('/login')
    },
    pullingUp () {
      this.page += 1
      this.getList()
    },
    async getList (loadMore = true) {
      const newList = await ArticleUser(this.ID);
      if (loadMore) {
        this.list = this.list.concat(newList)
      } else {
        this.list = newList
      }
      if (!this.beforePullDown) {
        this.beforePullDown = true
      }
      this.loadedAll = this.total <= this.list.length
    },
    chuancan(id) {
    this.$router.push({
          name: 'Adata',
          query: {
            id: id
          }
        })
    }
  }
}
</script>
<style lang="scss" scoped>
p{
  margin-block-start: 0;
  margin-block-end: 0;
}
.container{
  height: 100%;
  width: 100%;
  background: #f5f5f5;
  .list-wrap{
    height: calc(100% - 50px);
    overflow-y: hidden;
  }
}
.NOLogin{
  text-align: center;
}
.article-list {
  width: 100%;
  height: auto;
  box-sizing: border-box;
  padding: 15px 15px 0;
  .article {
    display: flex;
    width: 100%;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding: 10px 15px;
    box-sizing: border-box;
    background: #FFF;
    border-radius: 5px;
    box-shadow: 0 0 6px #e3e3e3;
    .left {
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 80px;
        height: 80px;
        border-radius: 50%;
      }
    }
    .right {
      padding-left: 15px;
      box-sizing: border-box;
      display: flex;
      width: 235px;
      height: 80px;
      flex-direction: column;
      align-items: flex-start;
      justify-content: space-around;
      p {
        width: 100%;
        line-height: 20px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 16px;
      }
      .more-info{
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 14px;
        color: #666;
      }
    }
  }
}

</style>
